package com.cyys.common.annotation;

import com.cyys.common.ql.AR;
import com.cyys.common.thirdInterface.bd.SpamUtil;
import com.cyys.common.utils.StringUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class SpamTextCheckd<T> {
	public AR validate(T t) throws Exception {
		Class<?> clazz = t.getClass();
		Field[] fields = clazz.getDeclaredFields();

		for (Field field : fields) {
			if (field.isAnnotationPresent(SpamAnnotation.class)) {
				SpamAnnotation spam = field.getAnnotation(SpamAnnotation.class);
				boolean html = spam.html();
				Method method = clazz.getDeclaredMethod("get" + StringUtil.upperCaseFirst(field.getName()));
				String text = (String) method.invoke(t);
				if(StringUtil.isNotEmpty(text)){
					if (html) {     //如果是html，去掉
						text = StringUtil.removeHtml(text);
					}
					AR ar = SpamUtil.checkText(text);
					if(ar.getCode() != 0){
						ar.setMsg(spam.name() + ar.getMsg());
						return ar;
					}
				}
			}
		}
		return AR.ok("验证成功！");
	}
}